<?php

require_once('classes/modeles/Metier/TypeGarde.php');
require_once('classes/modeles/Metier/Grade.php');
require_once('classes/modeles/Metier/Aptitude.php');

/**
 * Description of Metier
 *
 * @author ROCU Fabien
 */
class Metier {

    public $array_typeGarde_id;
    public $array_typeGarde;

    public $array_grade_id;
    public $array_grade;

    public $array_aptitude_id;
    public $array_aptitude;

    function __construct() {

    }

    public function charger_arrayTypeGardeID_metier() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT type_garde_id FROM type_garde";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['type_garde_id'];

                }

                $this->array_typeGarde_id = $tmp;
            }
            else
                throw new Exception('Erreur Metier: la création de la liste des identifiants de type de garde a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayTypeGarde_metier() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_typeGarde_id) ; $i++) {
            $typeGarde = new TypeGarde();
            $typeGarde->charger_typeGarde($this->array_typeGarde_id[$i]);

            $tmp[$this->array_typeGarde_id[$i]] = serialize($typeGarde);
        }

        $this->array_typeGarde = $tmp;
    }

    public function creer_typeGarde( $nom ) {
        $typeGarde = new TypeGarde();
        $typeGarde->set_typeGarde($nom);
        $id = $typeGarde->creer_typeGarde();

        $nb_typeGarde = count($this->array_typeGarde_id);
        $this->array_typeGarde_id[$nb_typeGarde] = $id;

        $this->array_typeGarde[$id] = serialize($typeGarde);
    }

    public function sauvegarder_typeGarde( $id, $nom ) {
        $typeGarde = unserialize($this->array_typeGarde[$id]);
        $typeGarde->set_typeGarde($nom);
        $typeGarde->sauvegarder_typeGarde();

        $this->array_typeGarde[$id] = serialize($typeGarde);
    }

    public function effacer_typeGarde( $id ) {
        $typeGarde = new TypeGarde();
        $typeGarde->effacer_typeGarde($id);

        unset($this->array_typeGarde[$id]);

        $this->charger_arrayTypeGardeID_metier();
    }

    /**************************************************************************/

    public function charger_arrayGradeID_metier() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT grade_id FROM grade";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['grade_id'];

                }

                $this->array_grade_id = $tmp;
            }
            else
                throw new Exception('Erreur Metier: la création de la liste des identifiants de grade a échoué');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayGrade_metier() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_grade_id) ; $i++) {
            $grade = new Grade();
            $grade->charger_grade($this->array_grade_id[$i]);

            $tmp[$this->array_grade_id[$i]] = serialize($grade);
        }

        $this->array_grade = $tmp;
    }

    public function creer_grade( $nom, $abbr, $rang ) {
        $grade = new Grade();
        $grade->set_grade($nom, $abbr, $rang);
        $id = $grade->creer_grade();

        $nb_grade = count($this->array_grade_id);
        $this->array_grade_id[$nb_grade] = $id;

        $this->array_grade[$id] = serialize($grade);
    }

    public function sauvegarder_grade( $id, $nom, $abbr, $rang ) {
        $grade = unserialize($this->array_grade[$id]);
        $grade->set_grade($nom, $abbr, $rang);
        $grade->sauvegarder_grade();

        $this->array_grade[$id] = serialize($grade);
    }

    public function effacer_grade( $id ) {
        $grade = new Grade();
        $grade->effacer_grade($id);

        unset($this->array_grade[$id]);

        $this->charger_arrayTypeGardeID_metier();
    }

    /**************************************************************************/

    public function charger_arrayAptitudeID_metier() {
        global $_mysql;

        $tmp = array();

        try {
            $query = "SELECT aptitude_id FROM liste_aptitude";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                for ($i = 0 ; $row = $_mysql->fetch_array($result) ; $i++) {
                    $tmp[$i] = $row['aptitude_id'];

                }
            }
            // else
            // throw new Exception('Erreur Metier: la création de la liste des identifiants d\'aptitude a échoué');

            $this->array_aptitude_id = $tmp;

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_arrayAptitude_metier() {
        $tmp = array();

        for ($i = 0 ; $i < count($this->array_aptitude_id) ; $i++) {
            $aptitude = new Aptitude();
            $aptitude->charger_aptitude($this->array_aptitude_id[$i]);

            $tmp[$this->array_aptitude_id[$i]] = serialize($aptitude);
        }

        $this->array_aptitude = $tmp;
    }

    public function creer_aptitude( $nom ) {
        $aptitude = new Aptitude();
        $aptitude->set_aptitude($nom);
        $id = $aptitude->sauvegarder_aptitude();

        $nb_aptitude = count($this->array_aptitude_id);
        $this->array_aptitude_id[$nb_aptitude] = $id;

        $this->array_aptitude[$id] = serialize($aptitude);
    }

    public function sauvegarder_aptitude( $id, $nom ) {
        $aptitude = unserialize($this->array_aptitude[$id]);
        $aptitude->set_aptitude($nom);
        $aptitude->sauvegarder_aptitude();

        $this->array_aptitude[$id] = serialize($aptitude);
    }

    public function effacer_aptitude( $id ) {
        $aptitude = new Aptitude();
        $aptitude->effacer_aptitude($id);

        unset($this->array_aptitude[$id]);

        $this->charger_arrayAptitudeID_metier();
    }

}
?>
